package com.nursing.Dao;

import com.nursing.bean.Elder;
import com.nursing.model.dto.ElderStatisticsDto;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface ElderDao {
    @Select("SELECT * FROM elder")
    List<Elder> getAll();

    @Select("SELECT * FROM elder WHERE id = #{id}")
    Elder getById(Integer id);

    @Insert("INSERT INTO elder (name, gender, age, id_card, phone, emergency_contact, emergency_phone,avatar,created_at) " +
            "VALUES (#{name}, #{gender}, #{age}, #{idCard}, #{phone}, #{emergencyContact}, #{emergencyPhone},#{avatar},#{createdAt})")
    @Options(useGeneratedKeys = true, keyProperty = "id")
    void insert( Elder elder);

    @Update("UPDATE elder SET name = #{name},avatar = #{avatar}, gender = #{gender}, age = #{age}, id_card = #{idCard}, phone = #{phone}, " +
            "emergency_contact = #{emergencyContact}, emergency_phone = #{emergencyPhone} WHERE id = #{id}")
    void update( Elder elder);

    @Delete("DELETE FROM elder WHERE id = #{id}")
    void delete(Integer id);

    @Select("SELECT MONTH(created_at) AS month, COUNT(id) AS count " +
            "FROM elder " +
            "WHERE YEAR(created_at) = YEAR(CURDATE()) " +
            "GROUP BY MONTH(created_at) " +
            "ORDER BY month")
    List<ElderStatisticsDto> getMonthlyCount();


    @Select("SELECT COUNT(*) FROM elder")
    Integer getCount();
}